package com.ccl.iot.privates;
public abstract class StorableGroup extends StorableObject {
	private static final long serialVersionUID = 1L;
		
	protected abstract Storable[] getMembers();
	
	private transient OnObjectChangeListener iListener = null;
	
	private void updateChangeListener(){
		Storable tMembers[] = getMembers();
		
		if (tMembers != null){
			for (int i = 0; i < tMembers.length; i++){
				if (tMembers[i] != null){
					tMembers[i].setOnObjectChangeListener(iListener);
				}
			}
		}
	}
	
	@Override
	public boolean setOnObjectChangeListener(OnObjectChangeListener aListener) {
		if (super.setOnObjectChangeListener(aListener)){
			if (aListener != null){
				if (iListener == null){
					iListener = new OnObjectChangeListener(){
						@Override
						public void onObjectChange(Storable aObject) {
							StorableGroup.this.onObjectChange();
						}
					};
					
					updateChangeListener();
				}
			}else{
				iListener = null;
				updateChangeListener();
			}
			
			return true;
		}
		
		return false;
	}
	
	protected void onMemberChange(Storable aFrom, Storable aTo){
		if (aFrom != null){
			aFrom.setOnObjectChangeListener(null);
		}
		
		if (aTo != null){
			aTo.setOnObjectChangeListener(iListener);
		}
	}
}
